home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / memstat.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-02  |  3.4 KB  |  156 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20. #ifndef _MEMSTAT_H_
  21. #define _MEMSTAT_H_
  22.  
  23. #include <assert.h>
  24. #include <sys/time.h>
  25. #include <unistd.h>
  26. #include <math.h>
  27.  
  28. #include <limits.h>
  29.  
  30. class MemoryRequest {
  31. public:
  32.  
  33.   MemoryRequest (void)
  34.     : _sec (LONG_MAX),
  35.       _usec (LONG_MAX),
  36.       _size (0),
  37.       _address (INVALID)
  38.   {}
  39.  
  40.   enum { FREE_OP = 0,
  41.      MALLOC_OP,
  42.      REALLOC_OP,
  43.      REFREE_OP,
  44.      ALLOCATE_OP,
  45.      DEALLOCATE_OP,
  46.      INVALID
  47.   };
  48.  
  49.   void malloc (void * addr,
  50.            size_t sz)
  51.   {
  52.     _size = sz;
  53.     _address = (unsigned long) addr | MALLOC_OP;
  54.     markTime (_sec, _usec);
  55.     // printf ("malloc %d (%f)\n", sz, getTime());
  56.   }
  57.  
  58.  
  59.   void free (void * addr)
  60.   {
  61.     _address = (unsigned long) addr | FREE_OP;
  62.     markTime (_sec, _usec);
  63.     // printf ("free %d (%f)\n", _address, getTime());
  64.   }
  65.  
  66.  
  67.   void allocate (int sz)
  68.   {
  69.     _address = ALLOCATE_OP;
  70.     _size = sz;
  71.     markTime (_sec, _usec);
  72.     // printf ("allocate %d (%f)\n", sz, getTime());
  73.   }
  74.  
  75.  
  76.   void deallocate (int sz)
  77.   {
  78.     _address = DEALLOCATE_OP;
  79.     _size = sz;
  80.     markTime (_sec, _usec);
  81.     // printf ("allocate %d (%f)\n", sz, getTime());
  82.   }
  83.  
  84.  
  85.   // Set sec & usec to the current time.
  86.   void markTime (long& sec, long& usec)
  87.   {
  88. #ifdef __SVR4 // Solaris
  89.     hrtime_t t;
  90.     t = gethrtime();
  91.     sec = *((long *) &t);
  92.     usec = *((long *) &t + 1);
  93. #else
  94.     struct timeval tv;
  95.     struct timezone tz;
  96.     gettimeofday (&tv, &tz);
  97.     sec = tv.tv_sec;
  98.     usec = tv.tv_usec;
  99. #endif
  100.   }
  101.  
  102.   int getType (void) {
  103.     return _address & 7;
  104.   }
  105.  
  106.   int getAllocated (void) {
  107.     return _size;
  108.   }
  109.  
  110.   int getDeallocated (void) {
  111.     return _size;
  112.   }
  113.  
  114.   unsigned long getAddress (void) {
  115.     return _address & ~7;
  116.   }
  117.  
  118.   int getSize (void) {
  119.     return _size;
  120.   }
  121.  
  122.   double getTime (void) {
  123.     return (double) _sec + (double) _usec / 1000000.0;
  124.   }
  125.  
  126.   long getSeconds (void) {
  127.     return _sec;
  128.   }
  129.  
  130.   long getUseconds (void) {
  131.     return _usec;
  132.   }
  133.  
  134.   friend int operator< (MemoryRequest& m, MemoryRequest& n) {
  135.     return ((m._sec < n._sec)
  136.         || ((m._sec == n._sec)
  137.         && (m._usec < n._usec)));
  138.   }
  139.  
  140.   friend int operator== (MemoryRequest& m, MemoryRequest& n) {
  141.     return ((m._sec == n._sec) && (m._usec == n._usec));
  142.   }
  143.  
  144.  
  145. private:
  146.   int    _size;         // in bytes 
  147.   unsigned long _address;  // The address returned by malloc/realloc   
  148.   long    _sec;          // seconds as returned by gettimeofday      
  149.   long    _usec;         // microseconds as returned by gettimeofday 
  150. };
  151.  
  152.  
  153.  
  154.  
  155. #endif // _MEMSTAT_H_ 
  156.